package cn.edu.swu.book;

import cn.edu.swu.core.BasicServlet;
import jakarta.servlet.ServletConfig;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.Writer;
import java.sql.SQLException;
import java.util.List;

/**
 * /book/add
 * /book/del
 * /book/update
 * /book/list
 */

@WebServlet(urlPatterns = "/book/*")
public class BookController extends BasicServlet {

    private BookService bookService = null;

    public void init(ServletConfig config) {
        this.bookService = BookService.getInstance();
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        String uri = request.getRequestURI();
        String method = uri.substring(uri.lastIndexOf("/")+1);
        System.out.println(method);

        try {
            switch (method) {
                case "list" -> this.list(request, response);
                case "add" -> this.add(request, response);
                case "delete" -> this.delete(request, response);
                case "edit" -> this.edit(request, response);
                case "update" -> this.update(request, response);
                case "search" -> this.search(request, response);
            }
        } catch (IOException | SQLException e) {
            throw new ServletException(e);
        }
    }

    private void list(HttpServletRequest request, HttpServletResponse response) throws ServletException{
        try {
            List<Book> books = this.bookService.getAll();
            try(Writer writer = response.getWriter()) {
                String table = this.bookService.wrapTable(books);
                String page  = this.wrapPage(table);
                writer.write(page);
            }
        } catch (SQLException | IOException e) {
            throw new ServletException(e);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }

    private void add(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String name = request.getParameter("name");
        String author = request.getParameter("author");
        double price = Double.parseDouble(request.getParameter("price"));
        String description = request.getParameter("description");
        String publish = request.getParameter("publish");

        try {
            this.bookService.save(name, author, price, description, publish);
        } catch (SQLException e) {
            throw new ServletException(e);
        }

        response.sendRedirect("./list");
    }

    private void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String bookId = request.getParameter("id");
        try {
            this.bookService.delete(Integer.parseInt(bookId));
        }catch (SQLException ex) {
            throw new ServletException(ex);
        }
        response.sendRedirect("./list");
    }

    private void edit(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        Book book = this.bookService.getBookById(id);
        try(Writer writer = response.getWriter()) {
            String form = this.bookService.wrapForm(book);
            String page  = this.wrapPage(form);
            writer.write(page);
        }
    }

    private void update(HttpServletRequest request, HttpServletResponse response) throws SQLException, IOException {
        int id = Integer.parseInt(request.getParameter("id"));
        String name = request.getParameter("name");
        String author = request.getParameter("author");
        double price = Double.parseDouble(request.getParameter("price"));
        String description = request.getParameter("description");
        String publish = request.getParameter("publish");

        this.bookService.updateBook(id, name, author, price, description, publish);
        response.sendRedirect("./list");
    }

    private void search(HttpServletRequest request, HttpServletResponse response) throws ServletException {
        String key = request.getParameter("key");
        try {
            List<Book> books = this.bookService.search(key);
            try(Writer writer = response.getWriter()) {
                String table = this.bookService.wrapTable(books);
                String page  = this.wrapPage(table);
                writer.write(page);
            }
        } catch (IOException e) {
            throw new ServletException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}
